package com.banmao.youZone.model;

import com.alibaba.fastjson2.JSON;
import com.banmao.youZone.entity.UserVO;
import com.banmao.youZone.mapper.RequestPropertyMapper;
import com.banmao.youZone.mapper.UserMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Slf4j
@Controller
public class IndexController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RequestPropertyMapper requestPropertyMapper;

    @GetMapping({"", "/", "/index", "/index/"})
    public String index(Model model) {

        List<UserVO> userVOS = userMapper.selectList(Wrappers.lambdaQuery());

        userVOS.forEach(u -> {
            String mail = u.getMail();
            if (StringUtils.isNotBlank(mail)) {
                int accountIdx = mail.indexOf("@");
                if (accountIdx <= -1) {
                    accountIdx = mail.length();
                }
                char[] charArray = mail.toCharArray();
                for (int i = 0; i < charArray.length; i++) {
                    if (i > 1 && i < accountIdx - 2) {
                        charArray[i] = 'X';
                    }
                }
                u.setMail(new String(charArray));
            }
        });

        model.addAttribute("users", JSON.toJSONString(userVOS));

        return "/index";
    }

    @GetMapping("/index/clock")
    public String clock(Model model) {

        return "/clock";
    }

    @GetMapping("/index/config")
    public String config(Model model) {
        return "/config";
    }


    @GetMapping("/index/history")
    public String history(Model model) {
        return "/history";
    }

}
